package org.example.common.dubbo.filter;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.example.common.core.constants.GrayConstant;
import org.example.common.core.utils.ribbon.GrayRequestContextHandler;
import org.example.common.core.utils.StringUtils;

/**
 * dubbo日志过滤器
 */
@Slf4j
@Activate(group = {CommonConstants.PROVIDER}, order = Integer.MAX_VALUE)
public class DubboGrayTagProviderFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String grayTag = RpcContext.getContext().getAttachment(GrayConstant.GRAY_HEADER);
        if(StringUtils.isNotEmpty(grayTag)){
            GrayRequestContextHandler.setGrayTag(Boolean.valueOf(grayTag));
        }
        return invoker.invoke(invocation);
    }

}
